<?php namespace system\tag;
use houdunwang\view\build\TagBase;

class Common extends TagBase {
	/**
	 * 标签声明
	 * @var array
	 */
	public $tags = [
		'prev'     => [ 'block' => false ] ,
		'next'     => [ 'block' => false ] ,
		'line' => [ 'block' => false ],
		'category'  => [ 'block' => true , 'level' => 4 ] ,
		'slide'  => [ 'block' => true , 'level' => 4 ] ,
		'arclist' => ['block' => true, 'level' => 4],
		'tag'  => [ 'block' => true , 'level' => 4 ] ,
	];
	/*
	 * 处理模块标签
	 * @param $attr
	 * @param $content
	 * @param $view
	 */
	//<tag action='links.links'></tag>
	//<tag action='模块.system/Tag/方法'></tag>
	public function _tag( $attr , $content , &$view)
	{
		$info  = explode('.',$attr['action']);
		$addons_module = $info[0];//调用那个模块处理
		$action = $info[1];//方法
		//看请求处理的模块是不是系统模块
		$moduleData = Db::table('module')->where('module_name',$addons_module)->first();
		$class = ($moduleData['module_is_system']==1 ? 'module': 'addons') .'\\' .$addons_module . '\system\Tag';
		return (new $class)->$action($attr , $content);
		//(new \addons\links\system\Tag())->links();
	}
	//line 标签:参数是：标签数形集合,标签嵌套内容只有快标签才有内容,视图服务对象；
	public function _line( $attr, $content, &$view ) {
		return 'link标签 测试内容';
	}
	//tag 标签,英语循环遍历首页中的导航条的分类，是使用的category中的表格
	public function _category ( $attr , $content , &$view )
	{
		$pid = isset( $attr[ 'pid' ] ) ? $attr[ 'pid' ] : -1;
		//看解析成了什么，看编译文件
		//转义不转义变量
		$str = <<<str
				<?php
					\$db= Db::table('category');
					if($pid>=0){
						\$db->where('cate_pid',$pid);
					}
					\$data = \$db->get();
					foreach(\$data as \$field):
						\$field['url'] = __ROOT__ . '/c/' . \$field['cate_id'] . '.html';
				?>
					$content
				<?php
					endforeach
				?>
str;

		return $str;
	}
	//定义轮播图的函数；是用来循环遍历轮播图的，使用轮播图的数据表
	public function _slide( $attr, $content, &$view ) {
		//看解析成了什么，看编译文件
		//转义不转义变量
		$str = <<<str
				<?php
					\$db= Db::table('slide');
					\$data = \$db->get();
					foreach(\$data as \$field):
					    \$field['slide_thumb'] = __ROOT__ . '/' .  \$field['slide_thumb'] ;
				?> 
					$content
				<?php
					endforeach
				?>
str;
		return $str;

	}//轮播图函数

	//文章列表函数定义标签:用于将左侧的课程安排的数据循环出来，就是文章的标题,所以使用的是
	public function _arclist ( $attr , $content , &$view )
	{
		$cid   = isset( $attr[ 'cid' ] ) ? $attr[ 'cid' ] : (Request::get('cate_id') ? : -1);
		$thumb = isset( $attr[ 'thumb' ] ) ? $attr[ 'thumb' ] : -1;
		$row   = isset( $attr[ 'row' ] ) ? $attr[ 'row' ] : -1;
		$arc_recommend   = isset( $attr[ 'arc_recommend' ] ) ? $attr[ 'arc_recommend' ] : -1;
		$str   = <<<str
				<?php
					\$db= Db::table('article');
					if('$cid'>=0){
					    \$db->whereIn('cate_id',explode(',','$cid'));
					}
					if($thumb==1){
					    \$db->where('arc_thumb','<>','');
					}
					if($arc_recommend==1){
					    \$db->where('arc_recommend','<>','');
					}
					if($row>=0){
						\$data = \$db->paginate($row);
					}else{
						\$data = \$db->get();
					}
					foreach(\$data as \$field):
					    \$field['arc_thumb'] = __ROOT__ . '/' .  \$field['arc_thumb'] ;
					    \$field['url'] = __ROOT__ . '/' .  \$field['arc_id'] . '.html' ;
				?> 
					$content
				<?php
					endforeach
				?>
str;

		return $str;
	}

	//下一篇
	public function _next ( $attr , $content , &$view )
	{
		$str = <<<str
			<?php
			\$arc_id = Request::get('arc_id');
			\$data = Db::table('article')->where('arc_id','>',\$arc_id)->orderBy('arc_id','ASC')->first();
			if(\$data){
				echo "<a href='".\$data['arc_id'].".html"."'>{\$data['arc_title']}</a>";
			}else{
				echo '无';
			}
			?>
str;

		return $str;
	}

	//上一篇
	public function _prev ( $attr , $content , &$view )
	{
		$str = <<<str
			<?php
			\$arc_id = Request::get('arc_id');
			\$data = Db::table('article')->where('arc_id','<',\$arc_id)->orderBy('arc_id','DESC')->first();
			if(\$data){
				echo "<a href='".\$data['arc_id'].".html"."'>{\$data['arc_title']}</a>";
			}else{
				echo '无';
			}
			?>
str;

		return $str;
	}
}